package org.arch_learn.test.service.impl;

import org.arch_learn.flashboot.context.Autowired;
import org.arch_learn.ioc_aop.annos.Service;
import org.arch_learn.ioc_aop.annos.Transactional;
import org.arch_learn.test.mapper.AccountMapper;
import org.arch_learn.test.po.Account;
import org.arch_learn.test.service.DemoService;

@Service(value = "demoService")
public class DemoServiceImpl implements DemoService {
    @Autowired(value = "accountMapper")
    private AccountMapper accountMapper;

    @Override
    public Account queryAccountByCardNo(String cardNo) {
        Account account = new Account();
        account.setCard_no(cardNo);
        return accountMapper.queryAccountByCardNo(account);
    }

    @Override
    @Transactional
    public void transfer(String from, String to, String amountStr) {
        Integer amount = Integer.valueOf(amountStr);
        Account fromAccount = queryAccountByCardNo(from);
        Account toAccount = queryAccountByCardNo(to);
        Integer fromMoney = fromAccount.getMoney();
        Integer toMoney = toAccount.getMoney();
        int fromDeposit = fromMoney - amount;
        int toDeposit = toMoney + amount;
        fromAccount.setMoney(fromDeposit);
        toAccount.setMoney(toDeposit);
        accountMapper.updateAccountByCardNo(fromAccount);
        if (fromDeposit < 0) throw new RuntimeException("余额不足！！！");
        accountMapper.updateAccountByCardNo(toAccount);
    }


}
